// 工程师端-注册
import Util from '../../utils/util.js'
import * as commAction from '../../commAction/index.js'
Util.page({
	/**
	 * 页面的初始数据
	 */
	data: {
		bgImgArr: ['../../images/login_bg.png', '../../images/choosePlayer.png','../../images/bg_game01.png','../../images/bg_game02.png','../../images/bg_game03.png'],
		wxUser: {},
		scene: 2, //0 启动页，1，开始页，2，3，4场景页，5结算页
		showAuthBT: false,
		authed: false,
		loadingPerc: 0,

		showMask: true,
		timeC: 3,

		//scene 1
		player: 0, //默认第一个角色
		playerImgArr: [],
		playerNameArr: ['../../images/role0.png', '../../images/role1.png', '../../images/role2.png'],
		
		//scene 2 , 3, 4
		playFailed:true


	},

	//正则将非数字替换位数字
	validInput: function() {

	},

	submit: function() {
		if(!this.data.valid) return;

	},

	setWxUser(userInfo) {
		if(!userInfo) {
			console.log('userInfo is null')
			return;
		}
		Util.setStorage('wx_user', userInfo)
		this.setData({
			authed: true,
			scene: 1,
			showAuthBT: false,
			wxUser: userInfo
		})

		Util.setStorage('wxUser', userInfo)

	},

	getUInfo(e) {
		var userInfo = e.detail.userInfo
		this.setWxUser(userInfo)

	},

	triggerFinish() {
		this.setData({
			showAuthBT: true
		})
	},

	loadingAni() {
		var timeid = setInterval(() => {
			this.data.loadingPerc += 20
			if(this.data.loadingPerc > 100) {
				this.data.loadingPerc = 100;
				clearInterval(timeid)
				this.triggerFinish()
			}

			this.setData({
				loadingPerc: this.data.loadingPerc
			})
		}, 500)
	},

	//scene 1

	choosePlayer(e) {
		var role = e.currentTarget.dataset.item * 1
		this.setData({
			player: role
		})

	},
	triggerStartGame() { //倒计时结束,进入游戏
		this.setData({
			showMask: false,
			scene:2
		})
	},

	startGame() {
		this.setData({
			timeC: 3,
			showMask: true
		})

		var self = this

		function startGameSub() {
			if(self.data.timeC <= 0) {
				self.triggerStartGame()
				return;
			}

			setTimeout(function() {
				console.log(self.data.timeC)

				self.setData({
					timeC: self.data.timeC - 1
				})
				startGameSub.call(self)

			}, 1000)
		}

		//		startGameSub()

		setTimeout(() => {
			self.setData({
				timeC: 2
			})
		}, 1000)

		setTimeout(() => {
			self.setData({
				timeC: 1
			})
		}, 2000)

		setTimeout(() => {
			self.triggerStartGame()
		}, 3000)

		//		var id = setInterval(() => {
		//			this.data.timeC -= 0.5;
		//			c++;
		//			
		//
		//			console.log(this.data.timeC)
		//
		//			if(c % 2 == 0) {
		//
		//				
		//			}
		//
		//		}, 500)
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.loadingAni()

		var wxUser = Util.getStorage('wxUser')
		if(wxUser) {
			this.setData({
				wxUser: wxUser
			})
		}
	},
	onShow() {

	},
	onReady() {

	}
})